/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package researchgrants.parts.GrantRequest.statusChanges;

import java.awt.Window;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import researchgrants.parts.GrantRequest.GrantRequest;
import researchgrants.parts.LoggedData.LoggedData;
import researchgrants.parts.db.Db;

/**
 * A GrantRequestStatusChange for Rejected grants.
 * Contains the rejection reason and decision date
 * @author DOStudent1
 */
public class GrantRequestStatusChangeRejected extends GrantRequestStatusChange {
    // unique identifiers
    final public static String dbString="Rejected";
    final public static String headerCaption="Rejection";

    static public GrantRequestStatusChangeRejected getByStatusChangeId(int statusChangeID) {
        return(new GrantRequestStatusChangeRejected(statusChangeID));
    }

    // This function creates a new rejection and adds it to the given grantRequest.
    static public GrantRequestStatusChangeRejectedView inputNew(Window parent, GrantRequest grantRequest, ActionListener savedActionListener, ActionListener cancelActionListener) {
        GrantRequestStatusChangeRejectedView newView=new GrantRequestStatusChangeRejectedView(parent, grantRequest, savedActionListener, cancelActionListener);
        return(newView);
    }

    private GrantRequestStatusChangeRejected(int statusChangeId) {
        super(statusChangeId);
        syncDataFromDb();
    }

    // the logged data
    private LoggedData rejectionReason, decisionDate;

    // populate the fields from the database
    private void syncDataFromDb() {
        try {
            Connection conn=Db.openDbConnection();
            PreparedStatement statusChangeStatement = Db.createPreparedStatement(conn, "SELECT RejectionReasonRef, DecisionDateRef FROM tblGrantRequestsStatusChangesRejected WHERE GrantRquestStatusChangeID=?");
            statusChangeStatement.setInt(1, getStatusChangeId());
            statusChangeStatement.executeQuery();
            ResultSet statusChangeResultSet = statusChangeStatement.getResultSet();
            Db.moveToFirstLine(statusChangeResultSet);
            rejectionReason=LoggedData.getByID((Integer)Db.getColumn(statusChangeResultSet,1));
            decisionDate=LoggedData.getByID((Integer)Db.getColumn(statusChangeResultSet,2));
            
            statusChangeResultSet.close();
            statusChangeStatement.close();
            Db.closeDbConnection(conn);
        } catch (SQLException ex) {
            Logger.getLogger(GrantRequestStatusChangeRejected.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            throw new IllegalArgumentException("Could not find id " + getStatusChangeId() + "\n" + ex.getLocalizedMessage());
        }
    }

    // Returns a view for the StatusChangeAwarded
    public GrantRequestStatusChangeView getView(Window parent) {
        return(new GrantRequestStatusChangeRejectedView(parent, this));
    }

    ///////////////////////////////////////////
    // return the type specific identifiers
    ///////////////////////////////////////////

    public String getStatusChangeTitle() {
        return(headerCaption);
    }

    public String getStatusChangeCode() {
        return(dbString);
    }
    
    //////////////////////////////////////////////
    // Retuns the raw fields. Used by the view
    //////////////////////////////////////////////

    LoggedData getLoggedDataDecisionDate() {
        return(decisionDate);
    }

    LoggedData getLoggedDataRejectionReason() {
        return(rejectionReason);
    }
    
}
